<template>
  <div class="register-container">
    <div class="register-box">
      <h2>创建账户</h2>
      <p class="register-desc">请输入以下信息完成注册</p>
      <div class="form-item">
        <label>用户名</label>
        <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="User" />
      </div>
      <div class="form-item">
        <label>邮箱</label>
        <el-input v-model="registerForm.email" placeholder="请输入邮箱" prefix-icon="Message" />
      </div>
      <div class="form-item">
        <label>密码</label>
        <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" show-password />
      </div>
      <el-button type="primary" class="register-button" @click="handleRegister" :loading="loading">注册</el-button>
      <div class="login-link">
        已有账户？ <router-link to="/login">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const registerForm = ref({
  username: '',
  email: '',
  password: ''
})
const loading = ref(false)

const handleRegister = async () => {
  if (!registerForm.value.username || !registerForm.value.email || !registerForm.value.password) {
    ElMessage.warning('请填写所有字段')
    return
  }

  loading.value = true
  try {
    await axios.post('/api/auth/register', registerForm.value)
    ElMessage.success('注册成功')
    router.push('/login')
  } catch (error) {
    ElMessage.error('注册失败：' + (error.response?.data?.message || '未知错误'))
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea, #764ba2);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.register-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease-in-out;
}

.register-box:hover {
  transform: translateY(-5px);
}

.register-box h2 {
  text-align: center;
  margin-bottom: 10px;
  font-size: 28px;
  color: #333;
  font-weight: 600;
}

.register-desc {
  text-align: center;
  color: #999;
  margin-bottom: 30px;
  font-size: 14px;
}

.form-item {
  margin-bottom: 24px;
}

.form-item label {
  display: block;
  margin-bottom: 6px;
  color: #333;
  font-size: 14px;
  font-weight: 500;
}

.el-input {
  height: 44px;
}

.el-input__inner {
  font-size: 14px;
  padding-left: 40px;
}

.register-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  transition: all 0.3s ease;
  background-color: #409EFF;
  border-color: #409EFF;
}

.register-button:hover {
  background-color: #5aacee;
}

.register-button:active {
  transform: scale(0.98);
}

.login-link {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #409EFF;
  text-decoration: none;
  font-weight: 500;
}

.login-link a:hover {
  text-decoration: underline;
}

/* 移动端适配 */
@media (max-width: 500px) {
  .register-box {
    width: 90%;
    padding: 24px;
  }

  .register-button {
    height: 40px;
  }

  .register-box h2 {
    font-size: 24px;
  }
}
</style>